﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Media;
using System.IO;

using Styx.Helpers;
using Styx;
using Styx.Plugins;
using Styx.Common;
using Styx.Common.Helpers;
using Styx.CommonBot;
using Styx.WoWInternals;
using Styx.CommonBot.Frames;
using Styx.WoWInternals.WoWObjects;

namespace Styx
{
    public class veryhuman : HBPlugin
    {
        private Random rnd = new Random();

        public override string Name { get { return "very human"; } }
        public override string Author { get { return "ikbeneenpeer"; } }
        public override Version Version { get { return new Version(1, 0); } }
        public override bool WantButton { get { return true; } }
        public string fileLoc = @"Plugins\veryhuman\Logs\" + StyxWoW.Me.RealmName + @"\" + StyxWoW.Me.Name + ".txt";

        private WoWPlayer Me = StyxWoW.Me;
        private WoWUnit Target;

        public bool checkMap = false;
        public int timeInMs1 = 0;
        public int timeInMs2 = 0;        

        public int stuckCnt = 0;
        private DateTime lStuck;

        public override void Pulse()
        {
            itemCount();
            //Logging.Write(System.Windows.Media.Colors.DarkRed, IsStuck().ToString());
            if (!Me.IsAlive) return;
            if (Me.Combat)
            {
                if (checkMap)
                {
                    Lua.DoString("RunMacroText('/click WorldMapFrameCloseButton');");
                    checkMap = false;
                }
                bool decide = decideMovement();
                if (decide)
                {
                    Logging.Write(System.Windows.Media.Colors.ForestGreen, "[very human] VERY FUCKING SUCCESFUL");
                }
                else if (!decide)
                {
                    Logging.Write(System.Windows.Media.Colors.ForestGreen, "[very human] VERY FUCKING CRASH");
                }
            }

            /*if (!IsStuck() && checkMap)
            {
                Logging.Write(System.Windows.Media.Colors.ForestGreen, "[very human] Unstuck! Closing map.");
                Lua.DoString("RunMacroText('/click WorldMapFrameCloseButton');");
                checkMap = false;
            }

            if (IsStuck() && !checkMap)
            {
                Logging.Write(System.Windows.Media.Colors.ForestGreen, "[very human] Stuck! Opening map.");
                Lua.DoString("RunMacroText('/click MiniMapWorldMapButton');");
                checkMap = true;
            }*/
        }

        public bool decideMovement()
        {
            timeInMs1 = rnd.Next(700, 1600);
            timeInMs2 = rnd.Next(900, 1700);
            WoWMovement.Move(WoWMovement.MovementDirection.StrafeLeft);
            Thread.Sleep(timeInMs1);
            WoWMovement.Move(WoWMovement.MovementDirection.StrafeRight);
            Thread.Sleep(timeInMs2);
            Logging.Write(System.Windows.Media.Colors.ForestGreen, "[very human] Time Left: " + timeInMs1 + " -- Time Right: " + timeInMs2);
            return true;
        }

        #region veryhuman Initialize
        public override void Initialize()
        {
            base.Initialize();
            createFile();
            lStuck = DateTime.Now;
            Logging.OnLogMessage += Logging_OnLogMessage;
            Logging.Write(System.Windows.Media.Colors.ForestGreen, "[very human] v" + Version.ToString() + " - Loaded");
        }

        public override void Dispose()
        {
            Logging.Write(System.Windows.Media.Colors.ForestGreen, "[very human] Disabled");
            base.Dispose();
        }
        #endregion

        public void Logging_OnLogMessage(System.Collections.ObjectModel.ReadOnlyCollection<Logging.LogMessage> messages)
        {
            foreach (Logging.LogMessage logMessage in messages)
            {
                ParseLogMessage(logMessage.Message);
            }

        }

        public void ParseLogMessage(string message)
        {
            if (message.Contains("We are stuck!!"))
            {
                if ((DateTime.Now - lStuck).TotalSeconds > 4)
                {
                    lStuck = DateTime.Now;
                    stuckCnt++;
                    Logging.Write(System.Windows.Media.Colors.DarkRed, "[very human] Detected a stuck at " + DateTime.Now);
                    writeFile(StyxWoW.Me);
                }
            }
        }

        #region veryhuman FileHandler
        public void createFile()
        {
            if (!Directory.Exists(@"Plugins\veryhuman\Logs\" + StyxWoW.Me.RealmName))
            {
                Directory.CreateDirectory(@"Plugins\veryhuman\Logs\" + StyxWoW.Me.RealmName);
                Thread.Sleep(2000);
                if (!File.Exists(fileLoc))
                    File.Create(fileLoc);
            }
            else if (!File.Exists(fileLoc))
                File.Create(fileLoc);
        }

        public void writeFile(WoWPlayer Me)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(fileLoc, true))
                {
                    stuckCnt++;
                    writer.WriteLineAsync(stuckCnt + "|" + Me.Name + "|" + Me.Level + "|" + Me.Location + "|" + DateTime.Now + "|");
                }
            }
            catch (Exception e)
            {
                Logging.Write(Name + " " + e.Message + Environment.NewLine + e.InnerException);
            }
        }
        #endregion

        

        private void itemCount()
        {
            List<string> _itemsToCount = new List<string>();
            _itemsToCount.Add("Searing Tongue");
            foreach (WoWItem item in StyxWoW.Me.BagItems)
            {
                if (_itemsToCount.Contains(item.Name))
                {
                    Logging.Write(item.StackCount.ToString());
                }
            }
        }
    }
}